import { RectNodeModel } from '@logicflow/core'
const message = useMessage()

class CustomNodeBil extends RectNodeModel {
  // 锚点的样式
  getAnchorStyle(anchorInfo) {
    const style = super.getAnchorStyle(anchorInfo)
    style.stroke = 'rgb(24, 125, 255)'
    style.r = 6
    style.hover.r = 8
    style.hover.fill = 'rgb(24, 125, 255)'
    style.hover.stroke = 'rgb(24, 125, 255)'
    return style
  }
  getConnectedSourceRules() {
    const rules = super.getConnectedSourceRules()
    const geteWayOnlyAsTarget = {
      validate: (source, target, sourceAnchor, targetAnchor) => {
        if (source.id === target.id) {
          message.warning('不能连接节点到自身！')
          return false
        }
        if (sourceAnchor.name === 'right' && targetAnchor.name === 'right') {
          message.warning('不能连接右侧锚点到另一个节点的右侧锚点！')
          return false
        }
        const edgesFromThisNode = source.graphModel.edges.filter(
          (edge) => edge.sourceNodeId === source.id
        )
        if (source.properties.inputType == 1) {
          const buttonId = sourceAnchor.id.split('_')[0]

          // 检查当前节点的按钮值是否有效
          const isValid = source.properties.menuButtons[buttonId]?.buttonValue?.length != 0
          if (!isValid) {
            message.warning('请检查按键的值')
            return false
          }

          // 检查当前节点的锚点是否已被使用
          const isAnchorUsed = edgesFromThisNode.some(
            (edge) => edge.sourceAnchorId === sourceAnchor.id
          )

          if (isAnchorUsed) {
            message.warning('当前节点的锚点只能连接一次！')
            return false
          }

          return true
        } else {
          // 检查当前节点的锚点是否已被使用
          const isAnchorUsed = edgesFromThisNode.some(
            (edge) => edge.startPoint.y === sourceAnchor.y
          )
          if (isAnchorUsed) {
            message.warning('当前节点的锚点只能连接一次！')
            return false
          }

          return true
        }
      }
    }
    rules.push(geteWayOnlyAsTarget)
    return rules
  }

  getDefaultAnchor() {
    const { width, height, x, y, id } = this
    let arr
    if (this.properties.inputType == 1) {
      arr = this.properties.menuButtons as any
    } else {
      arr = this.properties.matching as any
    }
    const rightArr: number[] = []
    const anchors = [
      {
        x: x - width / 2,
        y,
        name: 'left',
        id: `${id}_0`,
        edgeAddable: false //只能最为终点 不可作为启点
      }
    ]
    if (arr.length == 1) {
      arr.forEach((element: object, index: number) => {
        const obj: {} = {
          x: x + width / 2,
          y: y - 10,
          name: 'right',
          id: `${index}_2`
        }
        rightArr.push(obj)
      })
    } else {
      arr.forEach((element: object, index: number) => {
        const obj: {} = {
          x: x + width / 2,
          y: y - 10 + 56 * index - 24 * (arr.length - 1),
          name: 'right',
          id: `${index}_2`
        }
        rightArr.push(obj)
      })
    }
    const newArr = [...anchors, ...rightArr]
    return newArr
  }
}
export default CustomNodeBil
